home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15590 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.2 KB  |  119 lines

  1. Path: news.cuny.edu!not-for-mail
  2. From: dzielins@its.brooklyn.cuny.edu (Daniel Zielinski)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: help with queue
  5. Date: 19 Apr 1996 20:50:20 -0400
  6. Organization: Brooklyn College
  7. Message-ID: <4l9ccc$k0l@itsop2.its.brooklyn.cuny.edu>
  8. References: <4l73q1$148@itsop2.its.brooklyn.cuny.edu> <4l89sc$kf6@sparcserver.lrz-muenchen.de>
  9. NNTP-Posting-Host: itsop2.its.brooklyn.cuny.edu
  10.  
  11. >>this is QUEUE.c     
  12. >
  13. >>#include "QUEUE.h"
  14. >>#include <assert.h>
  15. >>#include <stdio.h>
  16. >
  17. >[insertQueue edited]
  18. >
  19. >>int
  20. >>emptyQueue(Queue q) {
  21. >>    assert((q.front==NULL && q.rear==NULL) || (q.front!=NULL && 
  22. >>        q.rear!=NULL)); 
  23. >>    return q.front==NULL;
  24. >>}
  25. >
  26. >This function obviously checks whether a queue is empty or not. It
  27. >returns 1 if the queue is empty, 0 if it is not empty.
  28. >
  29. >>DataType
  30. >>removeQueue(Queue *qp) {
  31. >>    Node *np;
  32. >>    DataType d;
  33. >
  34. >>    assert(!emptyQueue(*qp));     <-------- THIS IS THE LINE    
  35. >
  36. >The implementor of your queues states that his removeQueue() function
  37. >is not able to remove something from an empty queue. Since (s)he 
  38. >wants to return the value of the removed node, this is a sane 
  39. >assumption.
  40. >
  41. >>    np=qp->front;
  42. >>    d=np->data;
  43. >>    qp->front=np->next;
  44. >>    if (qp->front==NULL)
  45. >>        qp->rear=NULL;
  46. >>    deleteNode(np);
  47. >>    return d;
  48. >>}
  49. >
  50. >>void
  51. >>initQueue(Queue *qp) {
  52. >>    qp->front=qp->rear=NULL;
  53. >>}
  54. >
  55. >This creates an empty queue. Your implementation of a queue assumes
  56. >that only queues that are not empty can be passed to removeQueue().
  57. >
  58. >Kurt
  59. >--
  60. >| Kurt Watzka                             Phone : +49-89-2180-6254
  61. >| watzka@stat.uni-muenchen.de
  62.  
  63. Yes I know all of this. I know what each function does. The question is...
  64.  
  65. assertion in removeQueue fails, even though the QUEUE is not empty. (at least
  66. to my knowlege)
  67.  
  68. my test driver contains the following:
  69.  
  70.  
  71. #include <stdio.h>
  72. #include "node.h"
  73. #include "QUEUE.h"
  74.  
  75. main() {
  76.     Queue q1,q2;
  77.     int i=0;
  78.  
  79.     initQueue(&q1);
  80.     while (i<100) {
  81.         insertQueue(&q1,i);
  82.         i++;
  83.     }
  84.     i=0;
  85.     while(i<100) {
  86.         printf("%d\n",removeQueue(&q1));
  87.         i++;
  88.     }
  89. }
  90.  
  91. the program should print 1\n2\n3\n.... instead it prints assertion failed in QUEUE.c line 31 (removeQUEUE).
  92.  
  93. The Question is why does it happen? the QUEUE is not empty.
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.